/**
 * 程序访问入口
 *
 * */
//====================引入模块=================
const   express = require('express'),
        path = require('path'),
        logger = require('morgan'),
        bodyParser = require('body-parser'),
        cookieParser = require('cookie-parser'),
        compression = require('compression'),
        session = require('express-session'),

        DbOpt = require("./model/Dboption"),//初始化数据库连接

//====================引入路由=================
        user = require('./routes/user'),
        vps = require('./routes/vps'),
        role = require('./routes/role'),
        auth = require('./routes/auth'),
        client_user = require('./routes/client_user'),
        validate = require('./routes/validate'),
        cat = require('./routes/cat'),
        art = require('./routes/art'),
        slide = require('./routes/slide'),
        index=require('./routes/index'),
        ueditor = require("ueditor"),
        app = express();

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
    secret: '123456',
    cookie: { maxAge: 1000*3000 },
    resave: true,//是否允许session重新设置
    saveUninitialized: false,//session过期30分钟，，如果有人操作，每次以当前时间为起点，使用原设定的maxAge重设session过期时间到30分钟只有这种业务场景必须同行设置resave rolling为true.同时saveUninitialized要设置为false允许修改。
    rolling:true,//是否按照原设定的maxAge值重设session同步到cookie中
    name: 'gps'}))
//cookie处理
app.use(cookieParser());
app.use(compression({level: 9}));//优化压缩静态资源
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//app.use(express.static(path.join(__dirname, 'dist')));



app.use('/admin',express.static(path.join(__dirname, 'dist')));      //后台入口

/*  前台路由   end  */
app.use('/user',client_user);   //用户对外相关的接口
app.use('/art',art);   //文章对外相关的接口
app.use('/cat',cat);   //栏目对外相关的接口
app.use('/slide',slide);   //轮播图对外相关的接口
app.use('/',index);
app.use('/',validate); //验证会话是否超时

app.use('/auth',auth);   //权限相关的接口
app.use('/users',user);   //用户相关的接口
app.use('/vps',vps);   //用户相关的接口
app.use('/role',role);   //角色相关的接口








app.use("/ueditor/ue", ueditor(path.join(__dirname, 'public'), function(req, res, next) {
    // ueditor 客户发起上传图片请求
    var ActionType = req.query.action;
    var img_url = '/upload/img';
	var img_url2='/root/zaker/public/upload/img'
    if (ActionType === 'uploadimage' || ActionType === 'uploadfile' || ActionType === 'uploadvideo') {
        var dir_url=img_url
        if(ActionType === 'uploadvideo'){
            dir_url = '/upload/video';
        }
        if(ActionType == 'uploadfile'){
            dir_url = '/upload/file';
        }
        console.log('url:'+dir_url)
        res.ue_up(dir_url); //你只要输入要保存的地址 。保存操作交给ueditor来做
//        res.ue_up(img_url2);
	res.setHeader('Content-Type', 'text/html');
    }
    else if (req.query.action === 'listimage'){ //  客户端发起图片列表请求
        res.ue_list(img_url) // 客户端会列出 dir_url 目录下的所有图片
    } else {// 客户端发起其它请求
        res.setHeader('Content-Type', 'application/json');
        // 这里填写 ueditor.config.json 这个文件的路径
        res.redirect('/lib/UE/config.json')
    }
}));



// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.send(err)
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.send(err)
});



module.exports = app;
